<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Message\Controllers;

use App\Eloquent\Ygt\BuyersAddress;
use App\Eloquent\Ygt\BuyersInvoice;
use App\Eloquent\Ygt\Customer;
use App\Eloquent\Ygt\CustomerOrderPrice;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\Order;
use App\Eloquent\Ygt\OrderType;
use App\Eloquent\Ygt\Product;
use App\Eloquent\Ygt\ProductFields;
use App\Eloquent\Ygt\Purchase;
use App\Eloquent\Ygt\PurchaseMaterial;
use App\Eloquent\Ygt\SellerCompany;
use App\Eloquent\Ygt\UserMessage;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class OrderController extends Controller
{
    public $userId          = 25;
    //29下单人生产派单时给财务发消息
    //38=>'生产派单',//下单人生产派单时给发货员发消息
    public  function start()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $messageId          = request('message_id',0);
        if(!$userInfo || !$messageId)
        {
            xThrow( ERR_PARAMETER );
        }
        //消息内容
        $where              = ['id'=>$messageId];
        $message            = UserMessage::getInfo($where);
        if(!$message)
        {
            xThrow(ERR_UNKNOWN,'消息出错');
        }
        //消息关联的订单
        $type               = $message->type;
        //29=>'工单开票',//下单人生产派单时给财务发消息
        //38=>'生产派单',//下单人生产派单时给发货员发消息
        if(!in_array($type,[29,38]))
        {
            xThrow(ERR_UNKNOWN,'消息出错');
        }
        $typeId             = $message->type_id;
        $orderInfo          = Order::where('id',$typeId)->first();
        if(!$orderInfo)
        {
            xThrow(ERR_UNKNOWN,'工单信息出错');
        }
        $customerOrderId    = $orderInfo->customer_order_id;
        $customerOrderPrice = CustomerOrderPrice::where('customer_order_id',$customerOrderId)->first();
        if(!$customerOrderPrice)
        {
            xThrow(ERR_UNKNOWN,'价格计算出错');
        }
        $lists              = [];
        $price              = $customerOrderPrice->deal_price;
        $orderArr           = $orderInfo->toArray();
        $orderContent       = \App\Engine\OrderEngine::dealOrderFiled($orderArr);
        $productNum         = $orderContent['product_num'];
        $money              = $price * intval($productNum);
        $moneyFormat        = number_format($money, 2);
        $moneyTxt           = '¥'.$moneyFormat;
        $order              = [
            'order_type_title'=>$orderContent['order_type_title'],
            'product_name'=>$orderContent['product_name'],
            'finished_specification'=>$orderContent['finished_specification'],
            'product_num'=>$productNum,
            'money'=>$moneyTxt,
        ];
        $lists[]            = [
            'title'=>'工单信息',
            'data'=>[
                ['left'=>'工艺名称','right'=>$orderContent['order_type_title']],
                ['left'=>'品名','right'=>$orderContent['product_name']],
                ['left'=>'成品规格','right'=>$orderContent['finished_specification']],
                ['left'=>'数量','right'=>$productNum],
                ['left'=>'金额','right'=>$moneyTxt],
            ],
        ];
        //工单关联的客户
        $customerId         = $orderInfo->customer_name;
        $where              = ['id'=>$customerId];
        $customerInfo       = Customer::getInfo($where);
        if(!$customerInfo)
        {
            xThrow(ERR_UNKNOWN,'客户信息出错');
        }
        $imgPath            = Func::getImgUrlById($customerInfo->avatars_id);
        $customer           = [
            'customer_name'=>$customerInfo->customer_name,
            'img_path'=>$imgPath,
        ];
        //工单关联的开票信息
        $buyersInvoiceId    = $orderInfo->field_name_25;
        $where              = ['id'=>$buyersInvoiceId];
        $fields             = 'account_name,taxpayer_number,telphone,opening_bank,bank_card_number';
        $fields             .= ',province_name,city_name,area_name,address';
        $invoiceInfo        = BuyersInvoice::getInfo($where,$fields);
        $invoice            = [
            'account_name'=>'',
            'taxpayer_number'=>'',
            'telphone'=>'',
            'opening_bank'=>'',
            'bank_card_number'=>'',
            'province_name'=>'',
            'city_name'=>'',
            'area_name'=>'',
            'address'=>''
        ];
        if($invoiceInfo)
        {
            $invoice            = $invoiceInfo->toArray();
            $connectAddress     = $invoiceInfo->province_name.'-'.$invoiceInfo->city_name.'-'.$invoiceInfo->area_name;
            $lists[]            = [
                'title'=>'开票信息',
                'data'=>[
                    ['left'=>'名称','right'=>$invoiceInfo->account_name],
                    ['left'=>'电话','right'=>$invoiceInfo->telphone],
                    ['left'=>'纳税人识别号','right'=>$invoiceInfo->taxpayer_number],
                    ['left'=>'开户行','right'=>$invoiceInfo->opening_bank],
                    ['left'=>'账号','right'=>$invoiceInfo->bank_card_number],
                    ['left'=>'所在地区','right'=>$connectAddress],
                    ['left'=>'详细地址','right'=>$invoiceInfo->address],
                ]
            ];
        }
        $result             = [
            'type'=>$type,
            'order'=>$order,
            'customer'=>$customer,
            'invoice'=>$invoice,
            'lists'=>$lists,
        ];
        return $result;
    }
    //34=>'预发货',//工单创建完成时,下单人给发货员发消息
    public  function preSend()
    {
        $userId             = Func::getHeaderValueByName('userid',$this->userId);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $messageId          = request('message_id',0);
        if(!$userInfo || !$messageId)
        {
            xThrow( ERR_PARAMETER );
        }
        //消息内容
        $where              = ['id'=>$messageId];
        $message            = UserMessage::getInfo($where);
        if(!$message)
        {
            xThrow(ERR_UNKNOWN,'消息出错');
        }
        //消息关联的订单
        $type               = $message->type;
        //34=>'预发货',//工单创建完成时,下单人给发货员发消息
        if($type!=34)
        {
            xThrow(ERR_UNKNOWN,'消息出错');
        }
        $typeId             = $message->type_id;
        $orderInfo          = Order::where('id',$typeId)->first();
        if(!$orderInfo)
        {
            xThrow(ERR_UNKNOWN,'工单信息出错');
        }
        $lists              = [];
        $orderArr           = $orderInfo->toArray();
        $orderContent       = \App\Engine\OrderEngine::dealOrderFiled($orderArr);
        $productNum         = $orderContent['product_num'];
        $fieldName22        = intval($orderContent['field_name_22']);//收货地址
        $where              = ['id'=>$fieldName22];
        $buyersAddress      = BuyersAddress::getInfo($where);
        $addressAddress     = '';
        if($buyersAddress)
        {
            $addressAddress = $buyersAddress->province_name.$buyersAddress->city_name.$buyersAddress->area_name.$buyersAddress->address;
        }
        $lists[]            = [
            'title'=>'工单信息',
            'data'=>[
                ['left'=>'工艺名称','right'=>$orderContent['order_type_title']],
                ['left'=>'品名','right'=>$orderContent['product_name']],
                ['left'=>'成品规格','right'=>$orderContent['finished_specification']],
                ['left'=>'数量','right'=>$productNum],
                ['left'=>'交货日期','right'=>$orderContent['finished_date']],
                ['left'=>'收货地址','right'=>$addressAddress],
            ],
        ];
        //工单关联的客户
        $customerId         = $orderInfo->customer_name;
        $where              = ['id'=>$customerId];
        $customerInfo       = Customer::getInfo($where);
        if(!$customerInfo)
        {
            xThrow(ERR_UNKNOWN,'客户信息出错');
        }
        $imgPath            = Func::getImgUrlById($customerInfo->avatars_id);
        $customer           = [
            'customer_name'=>$customerInfo->customer_name,
            'img_path'=>$imgPath,
        ];
        $result             = [
            'type'=>$type,
            'customer'=>$customer,
            'lists'=>$lists,
        ];
        return $result;
    }
}